﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CreateMealPlan : System.Web.UI.Page
{
    PIMService pimService = new PIMService();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Userid"] == null)
        {
            Response.Redirect("index.aspx");
        }
        if (IsPostBack)
        {
            Session["RecipeOfMealPlan"] = "";
        }

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string userId = "";
        string[] arrayRecipeId = hdRecipeId.Value.Split(',');
        if (Session["Userid"] != null)
            userId = Session["Userid"].ToString();
        if (!string.IsNullOrEmpty(userId))
        {
            int _idUser = Convert.ToInt32(userId);
            double rating = 0;
            //Insert New MealPlan
            int mealPlanId = pimService.InsertPlanItMeal(_idUser, _idUser, _idUser, txtMealPlanName.Text, false, true, 4, 0, 0, "", 0, txtDescription.Text,txtBasicInfo.Text);
            //Insert Recipe
            if (mealPlanId > -1)
            {
                if (arrayRecipeId != null && arrayRecipeId.Length > 0)
                {
                    for (int i = 0; i < arrayRecipeId.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(arrayRecipeId[i]))
                        {
                            bool bResult = pimService.InsertPlanItMealDetail(mealPlanId, 1, Convert.ToInt32(arrayRecipeId[i]), 0, 1, 0, DateTime.Now, "", _idUser.ToString());
                        }
                    }

                    //Insert
                    Session["RecipeOfMealPlan"] = "";
                    Response.Redirect("MyMealPlan.aspx");
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "Error", "alert('Can not Create MealPlan')", true);
            }
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        //Response.Redirect("MyMealPlan.aspx");
        Session["RecipeOfMealPlan"] = "";
    }
}